home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / trace / tcpdump-2.2.1 / print-bootp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-03  |  6.2 KB  |  264 lines

  1. /*
  2.  * Copyright (c) 1988-1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that: (1) source code distributions
  7.  * retain the above copyright notice and this paragraph in its entirety, (2)
  8.  * distributions including binary code include the above copyright notice and
  9.  * this paragraph in its entirety in the documentation or other materials
  10.  * provided with the distribution, and (3) all advertising materials mentioning
  11.  * features or use of this software display the following acknowledgement:
  12.  * ``This product includes software developed by the University of California,
  13.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14.  * the University nor the names of its contributors may be used to endorse
  15.  * or promote products derived from this software without specific prior
  16.  * written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  *
  21.  * Format and print bootp packets.
  22.  */
  23. #ifndef lint
  24. static char rcsid[] =
  25.     "@(#) $Header: print-bootp.c,v 1.17 91/11/14 22:21:34 leres Exp $ (LBL)";
  26. #endif
  27.  
  28. #include <stdio.h>
  29.  
  30. #include <sys/param.h>
  31. #include <sys/types.h>
  32. #include <sys/socket.h>
  33. #include <net/if.h>
  34. #include <netinet/in.h>
  35. #include <netinet/if_ether.h>
  36. #include <strings.h>
  37. #include <ctype.h>
  38.  
  39. #include "interface.h"
  40. #include "addrtoname.h"
  41. #include "bootp.h"
  42.  
  43. void rfc1048_print();
  44. void cmu_print();
  45.  
  46. /*
  47.  * Print bootp requests
  48.  */
  49. void
  50. bootp_print(bp, length, sport, dport)
  51.     register struct bootp *bp;
  52.     int length;
  53.     u_short sport, dport;
  54. {
  55.     static char tstr[] = " [|bootp]";
  56.     static unsigned char vm_cmu[4] = VM_CMU;
  57.     static unsigned char vm_rfc1048[4] = VM_RFC1048;
  58.     u_char *ep;
  59.  
  60. #define TCHECK(var, l) if ((u_char *)&(var) > ep - l) goto trunc
  61.  
  62.     /* Note funny sized packets */
  63.     if (length != sizeof(struct bootp))
  64.         (void)printf(" [len=%d]", length);
  65.  
  66.     /* 'ep' points to the end of avaible data. */
  67.     ep = (u_char *)snapend;
  68.  
  69.     switch (bp->bp_op) {
  70.  
  71.     case BOOTREQUEST:
  72.         /* Usually, a request goes from a client to a server */
  73.         if (sport != IPPORT_BOOTPC || dport != IPPORT_BOOTPS)
  74.             printf(" (request)");
  75.         break;
  76.  
  77.     case BOOTREPLY:
  78.         /* Usually, a reply goes from a server to a client */
  79.         if (sport != IPPORT_BOOTPS || dport != IPPORT_BOOTPC)
  80.             printf(" (reply)");
  81.         break;
  82.  
  83.     default:
  84.         printf(" bootp-#%d", bp->bp_op);
  85.     }
  86.  
  87.     NTOHL(bp->bp_xid);
  88.     NTOHS(bp->bp_secs);
  89.  
  90.     /* The usual hardware address type is 1 (10Mb Ethernet) */
  91.     if (bp->bp_htype != 1)
  92.         printf(" htype-#%d", bp->bp_htype);
  93.  
  94.     /* The usual length for 10Mb Ethernet address is 6 bytes */
  95.     if (bp->bp_htype != 1 || bp->bp_hlen != 6)
  96.         printf(" hlen:%d", bp->bp_hlen);
  97.  
  98.     /* Only print interesting fields */
  99.     if (bp->bp_hops)
  100.         printf(" hops:%d", bp->bp_hops);
  101.     if (bp->bp_xid)
  102.         printf(" xid:0x%x", bp->bp_xid);
  103.     if (bp->bp_secs)
  104.         printf(" secs:%d", bp->bp_secs);
  105.  
  106.     /* Client's ip address */
  107.     TCHECK(bp->bp_ciaddr, sizeof(bp->bp_ciaddr));
  108.     if (bp->bp_ciaddr.s_addr)
  109.         printf(" C:%s", ipaddr_string(&bp->bp_ciaddr));
  110.  
  111.     /* 'your' ip address (bootp client) */
  112.     TCHECK(bp->bp_yiaddr, sizeof(bp->bp_yiaddr));
  113.     if (bp->bp_yiaddr.s_addr)
  114.         printf(" Y:%s", ipaddr_string(&bp->bp_yiaddr));
  115.  
  116.     /* Server's ip address */
  117.     TCHECK(bp->bp_siaddr, sizeof(bp->bp_siaddr));
  118.     if (bp->bp_siaddr.s_addr)
  119.         printf(" S:%s", ipaddr_string(&bp->bp_siaddr));
  120.  
  121.     /* Gateway's ip address */
  122.     TCHECK(bp->bp_giaddr, sizeof(bp->bp_giaddr));
  123.     if (bp->bp_giaddr.s_addr)
  124.         printf(" G:%s", ipaddr_string(&bp->bp_giaddr));
  125.  
  126.     /* Client's Ethernet address */
  127.     if (bp->bp_htype == 1 && bp->bp_hlen == 6) {
  128.         register struct ether_header *eh;
  129.         register char *e;
  130.  
  131.         TCHECK(bp->bp_chaddr[0], 6);
  132.         eh = (struct ether_header *)packetp;
  133.         if (bp->bp_op == BOOTREQUEST)
  134.             e = (char *)ESRC(eh);
  135.         else if (bp->bp_op == BOOTREPLY)
  136.             e = (char *)EDST(eh);
  137.         else
  138.             e = 0;
  139.         if (e == 0 || bcmp((char *)bp->bp_chaddr, e, 6) != 0)
  140.             printf(" ether %s", etheraddr_string(bp->bp_chaddr));
  141.     }
  142.  
  143.     TCHECK(bp->bp_sname[0], sizeof(bp->bp_sname));
  144.     if (*bp->bp_sname) {
  145.         printf(" sname ");
  146.         if (printfn(bp->bp_sname, ep)) {
  147.             fputs(tstr + 1, stdout);
  148.             return;
  149.         }
  150.     }
  151.     TCHECK(bp->bp_file[0], sizeof(bp->bp_file));
  152.     if (*bp->bp_file) {
  153.         printf(" file ");
  154.         if (printfn(bp->bp_file, ep)) {
  155.             fputs(tstr + 1, stdout);
  156.             return;
  157.         }
  158.     }
  159.  
  160.     /* Don't try to decode the vendor buffer unless we're verbose */
  161.     if (vflag <= 0)
  162.         return;
  163.  
  164.     TCHECK(bp->bp_vend[0], sizeof(bp->bp_vend));
  165.     printf(" vend");
  166.     if (bcmp(bp->bp_vend, vm_rfc1048, sizeof(u_long)) == 0)
  167.         rfc1048_print(bp->bp_vend, sizeof(bp->bp_vend));
  168.     else if (bcmp(bp->bp_vend, vm_cmu, sizeof(u_long)) == 0)
  169.         cmu_print(bp->bp_vend, sizeof(bp->bp_vend));
  170.     else {
  171.         u_long ul;
  172.  
  173.         bcopy((char *)bp->bp_vend, (char *)&ul, sizeof(ul));
  174.         printf("-#0x%x", ul);
  175.     }
  176.  
  177.     return;
  178. trunc:
  179.     fputs(tstr, stdout);
  180. #undef TCHECK
  181. }
  182.  
  183. void
  184. rfc1048_print(bp, length)
  185.     register u_char *bp;
  186.     int length;
  187. {
  188.     u_char tag;
  189.     u_char *ep;
  190.     register int i;
  191.     u_long ul;
  192.  
  193.     printf("-rfc1048");
  194.  
  195.     /* Step over magic cookie */
  196.     bp += sizeof(long);
  197.  
  198.     /* Setup end pointer */
  199.     ep = bp + length;
  200.  
  201.     while (bp < ep) {
  202.         tag = *bp++;
  203.         i = *bp++;
  204.         switch (tag) {
  205.  
  206.         case TAG_PAD:
  207.             /* no-op */
  208.             break;
  209.  
  210.         case TAG_SUBNET_MASK:
  211.             ul = 0;
  212.             bcopy((char *)bp, (char *)&ul, i);
  213.             printf(" SM:%s", ipaddr_string(&ul));
  214.             break;
  215.  
  216.         case TAG_TIME_SERVER:
  217.             ul = 0;
  218.             bcopy((char *)bp, (char *)&ul, i);
  219.             printf(" TS:%s", ipaddr_string(&ul));
  220.             break;
  221.  
  222.         case TAG_GATEWAY:
  223.             ul = 0;
  224.             bcopy((char *)bp, (char *)&ul, i);
  225.             printf(" G:%s", ipaddr_string(&ul));
  226.             break;
  227.  
  228.         case TAG_TIME_OFFSET:
  229.         case TAG_NAME_SERVER:
  230.         case TAG_DOMAIN_SERVER:
  231.         case TAG_LOG_SERVER:
  232.         case TAG_COOKIE_SERVER:
  233.         case TAG_LPR_SERVER:
  234.         case TAG_IMPRESS_SERVER:
  235.         case TAG_RLP_SERVER:
  236.         case TAG_HOSTNAME:
  237.         case TAG_BOOTSIZE:
  238.             printf(" tag-#%d", tag);
  239.             if (i == sizeof(long)) {
  240.                 bcopy((char *)bp, (char *)&ul, sizeof(long));
  241.                 printf(":0x%x", ul);
  242.             } else
  243.                 printf(":?");
  244.             break;
  245.  
  246.         case TAG_END:
  247.             return;
  248.  
  249.         default:
  250.             printf("[tag-#%d]", tag);
  251.             return;
  252.         }
  253.     }
  254. }
  255.  
  256. void
  257. cmu_print(bp, length)
  258.     register u_char *bp;
  259.     int length;
  260. {
  261.     /* XXX not really implemented */
  262.     printf("-cmu [...]");
  263. }
  264.